%{
	//#include "lexico.h" 
	#include <stdio.h>
	#include <stdlib.h>
	#include <math.h>
	#include <string.h>
	extern int yylex();
	extern char *yytext;
	extern FILE *yyin;
	extern int debugs;
	int yyerror(char *s);
	int nrolinea = 1;	
%}


%union {
	
	char *lexema;
	
}
	
%token <lexema> TID
%token TPARA
%token TPARC
%token TCRA
%token TCRC
%token TLLA
%token TLLC
%token TCOMA
%token TPCOMA
%token T2PUNTOS
%token TIGUAL
%token TASIGN
%token TPUNTO
%token TMAS
%token TMENOS
%token TMULT
%token TDIV
%token TAND
%token TOR
%token TMAY
%token TMEN
%token TMAYIG
%token TMENIG
%token TDIF

%token TARRAY
%token TBREAK
%token TDO
%token TELSE
%token TEND
%token TFOR
%token TIF
%token TIN
%token TLET
%token TNIL
%token TOF
%token TTHEN
%token TTO
%token TTYPE
%token TVAR
%token TWHILE 

%token <lexema> TCSTRING
%token <lexema> TCTEINT
	
%nonassoc TTHEN
%left TELSE
%nonassoc TASIGN
%left TOR
%left TAND
%nonassoc TIGUAL TDIF TMAY TMAYIG TMEN TMENIG
%left TMAS TMENOS
%left TMULT TDIV
%right TMENOSU
%nonassoc TDO
%nonassoc TOF

	
%%
/* -------------------------------------------------------------------------- */
prog : exp                                                                     
	 | decs
/* -------------------------------------------------------------------------- */	
exp : TPARA exp TPARC                                                                                     
	| lvalue                                                                                         
	| TPARA TPARC                                                                                                  
	| TNIL                                                                                                           
	| TCTEINT                                                                                                                  
	| TCSTRING
	/* operaciones */
	| exp TMAS exp					
	| exp TMENOS exp
	| exp TMULT exp
	| exp TDIV exp
	| exp TIGUAL exp
	| exp TDIF exp
	| exp TMAY exp
	| exp TMEN exp
	| exp TMAYIG exp
	| exp TMENIG exp
	| exp TAND exp
	| exp TOR exp
	/* ----------------------------- */
	| TMENOS exp %prec TMENOSU
	| type_id TLLA record_fields TLLC
	| lvalue TCRA exp TCRC TOF exp  
	| TPARA exp TPCOMA explist TPARC 
    /* asignacion */
	| lvalue TASIGN exp
	/* ----------------------------- */
	/* estructuras de control */
	| if_st
	| while_st
    | for_st
	| let_st
	| TBREAK 
	/* ----------------------------- */
	                  
	
/* -------------------------------------------------------------------------- */	
explist: exp
	   | exp TPCOMA explist
	   | exp error explist

/* -------------------------------------------------------------------------- */
record_fields : /* vacio */
			  | type_id TIGUAL exp TCOMA record_fields   
			  | type_id TIGUAL exp                                                                                      
			   
/* -------------------------------------------------------------------------- */																										
decs: dec decls                                                                                      
	
/* -------------------------------------------------------------------------- */
decls: /* vacio */
     | dec decls                                                                                       
	                                                                                                                              	
/* -------------------------------------------------------------------------- */	
dec : vardec                                                                                         
    | tydec                                                                                                 
	
/* -------------------------------------------------------------------------- */	
vardec : TVAR type_id TASIGN exp                                                          
	   | TVAR type_id T2PUNTOS type_id TASIGN exp
	   | error type_id TASIGN exp 
	   | error type_id T2PUNTOS type_id TASIGN exp 

/* -------------------------------------------------------------------------- */
tydec: TTYPE type_id TIGUAL ty
	 | error type_id TIGUAL ty

/* -------------------------------------------------------------------------- */
ty : type_id                                                                                                             
   | TARRAY TOF type_id                                                                            
   | TLLA tyfields TLLC                                                          

/* -------------------------------------------------------------------------- */
tyfields : /* vacio */
		 | tyfield TCOMA tyfields                          
		 | tyfield
		 | tyfield error tyfields 
                                                                                                                                       
/* -------------------------------------------------------------------------- */
tyfield : type_id T2PUNTOS type_id
	    | type_id error type_id 

/* -------------------------------------------------------------------------- */
type_id : TID
                                                                                                                                   
/* -------------------------------------------------------------------------- */
lvalue : type_id                                                                                                              
	   | lvalue TPUNTO type_id                                                                   
       | lvalue TCRA exp TCRC

/* -------------------------------------------------------------------------- */
let_st : TLET decs TIN TEND                                                               
	   | TLET decs TIN exp TEND                                       
       | TLET decs TIN exp TPCOMA explist TEND 

/* -------------------------------------------------------------------------- */
if_st : TIF exp TTHEN exp                                                               
      | TIF exp TTHEN exp TELSE exp  

/* -------------------------------------------------------------------------- */
while_st : TWHILE exp TDO exp
		 | TWHILE exp TDO error 
         
/* -------------------------------------------------------------------------- */
for_st : TFOR type_id TASIGN exp TTO exp TDO exp
	   | TFOR error TTO exp TDO exp 
	  

/* -------------------------------------------------------------------------- */
%%
int yyerror(char *s)
{
	
	if (strcmp(s,"syntax error") != 0)
		printf(" Error de Sintaxis en la linea %d: %s \n", nrolinea, s);
	else
		printf(" Error de Sintaxis en la linea %d, cerca al simbolo %s \n", nrolinea, yytext);
	return 0;
}
